﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Xml;
using Aandelendelen.Caching;

namespace Aandelendelen.TextLinks
{
	public class TextLinkCollection : Collection<TextLink>, ICacheable
	{
		public static string CacheName = "data_textlinks"; 

		public TextLinkCollection()
		{
			InitCacheKey();

			this.ReadTextLinks();
		}

		private void ReadTextLinks()
		{
			XmlDocument doc = new XmlDocument();

			string path = HttpContext.Current.Server.MapPath("/App_Data/TextLinks.xml");

			doc.Load(path);

			XmlNodeList nodes = doc.SelectNodes("/aandelendelen/links/link");

			foreach (XmlNode node in nodes)
			{
				this.ReadTextLink(node);
			}

		}

		private void ReadTextLink(XmlNode node)
		{
			string href = null;
			string text = null;
			string prefix = null;
			string suffix = null;

			string name = null;
			string email = null;

			bool nofollow = false;

			XmlAttribute attrStartDate = node.Attributes["startdate"];
			XmlAttribute attrEndDate = node.Attributes["enddate"];
			XmlAttribute attrName = node.Attributes["name"];
			XmlAttribute attrEmail = node.Attributes["email"];

			if (attrStartDate == null || attrEndDate == null)
				return;

			if (attrName != null)
				name = attrName.Value;

			if (attrEmail != null)
				email = attrEmail.Value;

			DateTime startDate = DateTime.Parse(attrStartDate.Value);
			DateTime endDate = DateTime.Parse(attrEndDate.Value);

			foreach (XmlNode childNode in node.ChildNodes)
			{
				switch (childNode.LocalName)
				{
					case "href":
						XmlAttribute hrefAttr = childNode.Attributes["url"];
						if (hrefAttr != null)
							href = hrefAttr.Value;

						XmlAttribute textAttr = childNode.Attributes["text"];
						if (textAttr != null)
							text = textAttr.Value;

						XmlAttribute prefixAttr = childNode.Attributes["prefix"];
						if (prefixAttr != null)
							prefix = prefixAttr.Value;

						XmlAttribute suffixAttr = childNode.Attributes["suffix"];
						if (suffixAttr != null)
							suffix = suffixAttr.Value;

						XmlAttribute nofollowAttr = childNode.Attributes["nofollow"];
						if (nofollowAttr != null)
							nofollow = XmlConvert.ToBoolean(nofollowAttr.Value);

						break;
				}
			}

			if (href != null && text != null)
			{
				TextLink textLink = new TextLink()
				{
					Href = href,
					Text = text,
					Prefix = prefix,
					Suffix = suffix,
					Name = name,
					Email = email,
					NoFollow = nofollow,
					StartDate = startDate,
					EndDate = endDate
				};

				{
					this.Add(textLink);
				}
			}
		}

		public void NotifyOnExpire()
		{
			foreach (TextLink link in this)
				link.CheckExpired();
		}

		public static TextLinkCollection GetInstance()
		{
			return DataCache.GetCacheableObject<TextLinkCollection>(CacheName);
		}

		public static TextLinkCollection GetInstance(string cacheKey)
		{
			return DataCache.GetCacheableObject<TextLinkCollection>(cacheKey);
		}

		#region ICacheable
		public CacheKey CacheKey { get; set; }

		public void CacheThis(string cacheKey)
		{
			this.CacheKey.CacheThis(cacheKey);
		}

		public void InitCacheKey()
		{
			CacheKey = new CacheKey()
			{
				CacheDependency = new CacheDependency(HttpContext.Current.Server.MapPath("/App_Data/TextLinks.xml")),
				SecondsBeforeInvalidate = 86400,
				ObjectToCache = this
			};
		}
		#endregion
	}
}